﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BoYuan.MvcCodeGenerator.Controllers;
using FineUIMvc;

namespace BoYuan.MvcCodeGenerator.Areas.SqlSugar.Controllers
{
    public class SetCodeController : BaseController
    {
        // GET: SqlSugar/SetCode
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Button_save_OnClick(FormCollection fc)
        {
            var db = MyInstance();
            var tableList = fc["hd_tables"].Split(',');

            string classTemplate = @"
{using}
namespace {Namespace}
{
{ClassDescription}{SugarTable}
    public partial class {ClassName}
    {
           public {ClassName}()
           {
{Constructor}
           }
{PropertyName}
    }
}
";

            try
            {
                //var list=
                db.DbFirst
                    .IsCreateAttribute(true)
                    .IsCreateDefaultValue(true)
                    .Where(p => tableList.Contains(p))
                    //改写默认模板
                    .SettingClassTemplate(p => p = classTemplate)
                    //.SettingClassDescriptionTemplate(p => p = "ClassDescriptionTemplate")
                    //.SettingConstructorTemplate(p => p = "ConstructorTemplate")
                    //.SettingNamespaceTemplate(p => p = "NamespaceTemplate")
                    //.SettingPropertyDescriptionTemplate(p => p = "PropertyDescriptionTemplate")
                    //.SettingPropertyTemplate(p => p = "PropertyTemplate")
                    //.ToClassStringList(txb_nameSpace.Text.Trim());
                    .CreateClassFile(fc["txb_path"].Trim(), fc["txb_nameSpace"].Trim());


                //var tbList = db.DbMaintenance.GetTableInfoList();//获取数据所有信息
                //var colmList = db.DbMaintenance.GetColumnInfosByTableName(tbList[0].Name);//获取所有字段信息，根据表名称
                //var bsList = db.DbMaintenance.GetDataBaseList(db);//获取所有数据库名称
                //var property=  db.Ado.DbBind.GetPropertyTypeName(colmList[1].DataType);//根据数据字段类型，生成C#对应的类型(例:nvarchar对应string)

                NotifyInformation("生成代码成功");
            }
            catch (Exception ex)
            {
                NotifyError("生成代码有问题：" + ex.Message);
            }

            return UIHelper.Result();
        }
    }
}